var content = document.getElementById("content");
var tuichu = "再按一次退出程序";
var parentView;
var insertDiv = document.getElementById("insertDiv");
var countNum = 0;
var isShishi = false;
var pageNum = 1;
var itemNum = 20;

(function($) {

	mui.init({
		keyEventBind: {
			backbutton: true //开启back按键监听
		}
	});
	setPxPerRem();
	mui.plusReady(function() {
		showEle(content);
		plus.navigator.setStatusBarBackground('#00367C');
		var swiper = new Swiper('.swiper-container', {
			pagination: '.swiper-pagination',
			paginationClickable: true,
			lazyLoading: true,
			autoplay: 2000,
		});
		//		if(isHaveNet()) {
		//			getIndexList();
		//		} else {
		//			mui.toast("无网络连接！")
		//		}

	});
	var backButtonPress = 0;

	mui.back = function(event) {
		backButtonPress++;
		if(backButtonPress > 1) {
			plus.runtime.quit();
		} else {
			plus.nativeUI.toast(tuichu);
		}
		setTimeout(function() {
			backButtonPress = 0;
		}, 1000);
		return false;
	};
})(mui)

mui("#queryType").on('click', 'div', function() {
	var imgSrc = this.getElementsByTagName("img")[0].src;
	var textStr = "";
	if(imgSrc.indexOf("01") > -1) {
		textStr = "入库查询";
	} else if(imgSrc.indexOf("02") > -1) {
		textStr = "出库查询";
	} else if(imgSrc.indexOf("03") > -1) {
		textStr = "库存查询";
	} else if(imgSrc.indexOf("04") > -1) {
		textStr = "回执状态";
		var isKehu = localStorage.getItem("isKehu");
		console.log(isKehu);
		if(isKehu != "0"){
			mui.toast("当前用户没有权限");
			return;
		} 
	}
 
	var postData = {
		pageflag: "page_main.html",
		isShishi: isShishi,
		textStr: textStr,
		itemID: this.id
	}

	var change = {
		pageUrl: this.id,
		postData: postData,
		aniShow: "fade-in",
		createNew: true
	}
	pageChange(change);

});

/**
 * 下拉刷新业务实现
 */
var isRefresh = false;

function pulldownRefresh() {
	pageNum = 1;
	isRefresh = true;
	removeLi("buildList");
	if(isHaveNet()) {
		getIndexList();
	} else {
		mui.toast("无网络连接！")
	}

}
/**
 * 上拉加载
 */
var isLoad = false;

function pullupRefresh() {
	isLoad = true;
	if(isHaveNet()) {
		getIndexList();
	} else {
		mui.toast("无网络连接！")
	}
}

$("#insertDiv").click(function() {
	var postData = {
		pageflag: "page_main.html",
	}

	var change = {
		pageUrl: 'page_policy.html',
		postData: postData,
		aniShow: "fade-in",
		createNew: true
	}
	pageChange(change);
});

function getIndexList() {
	var token = localStorage.getItem("token");

	var p = {
		op: "message",
		token: token,
		data: {
			unread: "0",
			page: "1",
			perpage: "20"
		}
	}
	var jsonData = {
		p: JSON.stringify(p)
	}
	plus.nativeUI.showWaiting();
	checkParams(jsonData);

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	};
	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		if(!isEmpty(data)) {

			localStorage.setItem("frist", JSON.stringify(data));
			for(var i = 0; i < data.list.length; i++) {
				if(i < 3) {
					createIndexItem(data.list[i]);
				}
			}
		}

		//		if(isRefresh) {
		//			mui('#content').pullRefresh().endPulldown();
		//			isRefresh = false;
		//		}
		//
		//		if(isLoad) {
		//			isLoad = false;
		//			mui('#content').pullRefresh().endPullup((pageNum > countNum));
		//		}

		//		if(pageNum == 1) {
		//			showEle(content);
		//			mui('#content').pullRefresh().endPullup(pageNum > countNum);
		//		}
		//		pageNum++;

		mui("#buildList").on('click', 'li', function() {

			var positionPoint = this.getElementsByTagName("div")[0];

			hiddenEle(positionPoint);

			var itemID = this.id;
			var postData = {
				pageflag: "page_index.html",
				itemID: itemID
			}

			var change = {
				pageUrl: 'page_policyinfo.html',
				postData: postData,
				aniShow: "fade-in"
			}
			pageChange(change);
		});

	});
}
/**
 * 创建首页条目
 * @param {Object} row 首页item数据
 */
function createIndexItem(row) {
	var isHidden = "mui-hidden";
	if(!isEmpty(row.status) && row.status == "0") {
		isHidden = "";
	}

	var createLi = document.createElement("li");
	createLi.className = "";
	createLi.id = row.msg_id;
	createLi.innerHTML = '<div class="message-position ' + isHidden + '"></div>' +
		'<div class="policy-title">' + row.title + '</div>' +
		'<div class="poilcy-time">' + row.add_time + '</div>';
	document.body.appendChild(createLi);
	insertBefore(createLi, insertDiv);
}

$("#setting").click(function() {

	var postData = {
		pageflag: "page_index.html",
	}

	var change = {
		pageUrl: 'page_me.html',
		postData: postData,
		aniShow: "fade-in"
	}
	pageChange(change);

});